package com.jeasonzhao.report.model.chart;

public class DataPointMatrix implements java.io.Serializable
{
	private static final long serialVersionUID=1L;
    private java.util.Vector<DataPointAry> m_vectData = null;
    public DataPointMatrix()
    {
        super();
    }

    public static DataPointMatrix fromArray(DataPointAry[] arys)
    {
        if(null == arys)
        {
            return null;
        }
        DataPointMatrix ret = new DataPointMatrix();
        for(int n = 0;n < arys.length;n++)
        {
            ret.addElement(arys[n]);
        }
        return ret;
    }

    public static DataPointMatrix fromList(java.util.List<DataPointAry> list)
    {
        if(null == list)
        {
            return null;
        }
        DataPointMatrix ret = new DataPointMatrix();
        for(int n = 0;n < list.size();n++)
        {
            ret.addElement((DataPointAry) list.get(n)); //may cause a cast-exception.
        }
        return ret;
    }

    public void addAll(DataPointMatrix colls)
    {
        if(null == colls || colls.m_vectData == null)
        {
            return;
        }
        initVector();
        m_vectData.addAll(colls.m_vectData);

    }

    public void addAll(java.util.List<DataPointAry> colls)
    {
        if(null == colls)
        {
            return;
        }
        initVector();
        for(int n = 0;n < colls.size();n++)
        {
            this.add((DataPointAry) colls.get(n));
        }
    }

    public void addAll(DataPointAry[] colls)
    {
        if(null == colls)
        {
            return;
        }
        initVector();
        for(int n = 0;n < colls.length;n++)
        {
            this.add(colls[n]);
        }
    }

    private void initVector()
    {
        if(null == m_vectData)
        {
            synchronized(this)
            {
                m_vectData = new java.util.Vector<DataPointAry>();
            }
        }
    }

    public void compress()
    {
        for(int n = 0;null != m_vectData && n <= m_vectData.size();n++)
        {
            if(m_vectData.elementAt(n) == null)
            {
                m_vectData.remove(n);
                n = n == 0 ? 0 : n - 1;
            }
        }
    }

    public boolean contains(Object obj)
    {
        for(int n = 0;null != m_vectData && n < m_vectData.size();n++)
        {
            if(m_vectData.elementAt(n).equals(obj))
            {
                return true;
            }
        }
        return false;
    }

    public DataPointAry elementAt(int nIndex)
    {
        return null == m_vectData ? null : (DataPointAry) m_vectData.elementAt(nIndex);
    }

    public DataPointAry get(int nIndex)
    {
        return elementAt(nIndex);
    }

    public java.util.Iterator<DataPointAry> iterator()
    {
        return null == m_vectData ? null : m_vectData.iterator();
    }

    public void addElement(DataPointAry value)
    {
        if(null == value)
        {
            return;
        }
        initVector();
        m_vectData.addElement(value);
    }

    public void add(DataPointAry value)
    {
        addElement(value);
    }

    public void insertElementAt(DataPointAry value,int nIndex)
    {
        initVector();
        m_vectData.insertElementAt(value,nIndex);
    }

    public void clear()
    {
        if(null != m_vectData)
        {
            m_vectData.clear();
        }
    }

    public void removeElementAt(int nIndex)
    {
        if(null == m_vectData || nIndex < 0 || nIndex >= m_vectData.size())
        {
            return;
        }
        m_vectData.removeElementAt(nIndex);
    }

    public void removeElement(DataPointAry item)
    {
        if(null == m_vectData)
        {
            return;
        }
        m_vectData.removeElement(item);
    }

    public void remove(DataPointAry item)
    {
        removeElement(item);
    }

    public void remove(int nIndex)
    {
        removeElementAt(nIndex);
    }

    public int indexOf(DataPointAry value)
    {
        if(null == m_vectData || null == value)
        {
            return -1;
        }
        return m_vectData.indexOf(value);
    }

    public int size()
    {
        return null == m_vectData ? 0 : m_vectData.size();
    }

    public DataPointAry[] toArray()
    {
        if(null == m_vectData)
        {
            return null;
        }
        DataPointAry[] ret = new DataPointAry[m_vectData.size()];
        m_vectData.copyInto(ret);
        return ret;
    }

    public void sort(java.util.Comparator<Object> comp)
    {
        if(null == m_vectData || null == comp || m_vectData.size() < 1)
        {
            return;
        }
        DataPointAry[] items = this.toArray();
        java.util.Arrays.sort(items,comp);
        m_vectData.clear();
        for(int n = 0;n < items.length;n++)
        {
            m_vectData.addElement(items[n]);
        }
    }

    public String toString()
    {
        return this.toString("\r\n",false);
    }

    public String toString(String str,boolean appendAtFirst)
    {
        StringBuffer strRet = new StringBuffer();
        for(int n = 0;n < this.size();n++)
        {
            if(str != null && (n > 0 || appendAtFirst))
            {
                strRet.append(str);
            }
            strRet.append(this.elementAt(n));
        }
        return strRet.toString();

    }

}
